use lazy_static::lazy_static;
use rayon::{ThreadPool, ThreadPoolBuilder};

// 自定义线程池
lazy_static! {
    pub static ref POOL: ThreadPool = ThreadPoolBuilder::new()
        .num_threads(num_cpus::get())
        .build()
        .unwrap();
}

/// 自定义线程池
fn main() {
    // 获取一个线程并执行。同步的
    POOL.install(|| {
        println!("Hello from a custom thread pool!");
        // 休眠2秒
        std::thread::sleep(std::time::Duration::from_secs(2));
        println!("Hello from a custom thread pool! end");
    });
    println!("main thread a");
    
    // 这个是异步的
    POOL.spawn(|| {
        std::thread::sleep(std::time::Duration::from_secs(5));
        println!("spawn thread");
    });
    
    println!("main thread b: {}", POOL.current_num_threads());

    std::thread::sleep(std::time::Duration::from_secs(10));
   
    
    /*POOL.scope(|s| {
        s.spawn(|_| {
            println!("spawn thread");
            std::thread::sleep(std::time::Duration::from_secs(2));
        });
    });*/

    /*POOL.install(|| {
        // 在这个线程池中执行的代码。并发执行
        (0..100).into_par_iter()
            .for_each(|i| println!("number: {}", i));
    });*/
}